package cn.player.act.filter;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.startchat.filter.camera.CameraRecordRenderer;
import com.startchat.filter.encode.EncoderConfig;
import com.startchat.filter.encode.VideoRecordEncoderTask;
import com.startchat.filter.filter.FilterManager;
import com.startchat.filter.widget.CameraSurfaceView;

import java.io.File;

import cn.player.R;
import cn.player.adapter.VideoFilterAdapter;

public class VideoRecordActivity extends AppCompatActivity implements View.OnClickListener {

    private CameraSurfaceView mCameraSurfaceView;
    private Button mRecordButton;
    private boolean mIsRecordEnabled;

    private ImageView mIvCameraSwitch;

    private LinearLayout mLlBottom;
    private Button mBtnFilter;
    private Button mBtnFilterToneCurve;
    private RecyclerView mRcvFilter;
    private VideoFilterAdapter mVideoFilterAdapter;
    private FilterManager.FilterType mCurrentFilterType;
    /**
     * 当前选中的FilterType
     */
    private int mFilterChangePosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_record);
        mCameraSurfaceView = (CameraSurfaceView) findViewById(R.id.camera);

        mIvCameraSwitch = (ImageView) findViewById(R.id.btn_camera_switch);
        mLlBottom = (LinearLayout) findViewById(R.id.ll_bottom);
        mRcvFilter = (RecyclerView) findViewById(R.id.rcv_filter_list);
        mBtnFilter = (Button) findViewById(R.id.filter_name);
        mBtnFilterToneCurve = (Button) findViewById(R.id.filter_tone_curve);
        mRecordButton = (Button) findViewById(R.id.record);

        mIvCameraSwitch.setOnClickListener(this);
        mBtnFilter.setOnClickListener(this);
        mBtnFilterToneCurve.setOnClickListener(this);
        mRecordButton.setOnClickListener(this);
        mCameraSurfaceView.setOnClickListener(this);

        mIsRecordEnabled = VideoRecordEncoderTask.getInstance(getApplicationContext()).isRecording();
        updateRecordButton();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCameraSurfaceView.onResume();
        updateRecordButton();
    }

    @Override
    protected void onPause() {
        mCameraSurfaceView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_camera_switch:
                mCameraSurfaceView.getRenderer().switchCamera();
                break;
            case R.id.filter_name:
                animHide(mLlBottom, getResources().getDimensionPixelOffset(R.dimen.view_height_60));
                animShow(mRcvFilter, getResources().getDimensionPixelOffset(R.dimen.view_height_80));
                setFilterAdapter();
                break;
            case R.id.filter_tone_curve:
                mCameraSurfaceView.changeFilter2(FilterManager.FilterType.ToneCurve);
                break;
            case R.id.camera:
                if (mRcvFilter.getTranslationY() == 0) {
                    animHide(mRcvFilter, getResources().getDimensionPixelOffset(R.dimen.view_height_80));
                    animShow(mLlBottom, getResources().getDimensionPixelOffset(R.dimen.view_height_60));
                }
                break;
            case R.id.record:
                if (!mIsRecordEnabled) {
                    mCameraSurfaceView.queueEvent(new Runnable() {
                        @Override
                        public void run() {
                            CameraRecordRenderer renderer = mCameraSurfaceView.getRenderer();
                            File outputFile = new File(getExternalCacheDir(), "rec_test_record_" + System.currentTimeMillis() + ".mp4");
                            if (outputFile.exists()) {
                                outputFile.delete();
                            }
                            renderer.setEncoderConfig(new EncoderConfig(outputFile, 720, 1280, 2000000));
                        }
                    });
                }
                mIsRecordEnabled = !mIsRecordEnabled;
                mCameraSurfaceView.queueEvent(new Runnable() {
                    @Override
                    public void run() {
                        mCameraSurfaceView.getRenderer().setRecordingEnabled(mIsRecordEnabled);
                    }
                });
                updateRecordButton();
                break;
        }
    }

    public void updateRecordButton() {
        mRecordButton.setText(
                getString(mIsRecordEnabled ? R.string.record_stop : R.string.record_start));
    }


    private void setFilterAdapter() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRcvFilter.setLayoutManager(linearLayoutManager);
        if (mVideoFilterAdapter == null) {
            mVideoFilterAdapter = new VideoFilterAdapter(this);
            mRcvFilter.setAdapter(mVideoFilterAdapter);
            mVideoFilterAdapter.setOnFilterConfigClickListener(new VideoFilterAdapter.OnFilterConfigClickListener() {
                @Override
                public void onFilterConfigClick(int position, FilterManager.FilterType filterType) {
                    if (mFilterChangePosition != position) {
                        setFilterBg(position, mFilterChangePosition);
                        mFilterChangePosition = position;
                        mCameraSurfaceView.changeFilter(filterType);
                    }
                }
            });
        } else {
            mVideoFilterAdapter.notifyDataSetChanged();
        }
    }

    private void setFilterBg(int newPosition, int currentPosition) {
        mVideoFilterAdapter.setCurrentPosition(newPosition);
        mVideoFilterAdapter.notifyItemChanged(currentPosition);
        mVideoFilterAdapter.notifyItemChanged(newPosition);
    }


    private void animHide(View view, int height) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, height);
        animator.setDuration(350);
        animator.start();
    }

    private void animShow(View view, int height) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", height, 0);
        animator.setDuration(350);
        animator.start();
    }


}
